package com.huarui.sale.action;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.ewaf.framework.action.BaseAction;
import com.huarui.sale.button.SaleButtonService;
import com.huarui.sale.model.Sale;
import com.huarui.sale.service.SaleService;
import com.huarui.sale.util.SaleConstantEnum;
import com.huarui.user.model.SessionMessage;
import com.huarui.user.security.UserHolder;

public class SaleAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6486493456838211061L;

	private Logger log = Logger.getLogger(SaleAction.class);
	private SessionMessage sessionMessage;
	private String queryParam;
	private JSONObject jsonObject;
	private SaleService saleService;
	private SaleButtonService saleButtonService;
    /**
     * 获取初始化按钮
     * @return
     */
	public String getSaleInitButtons(){
		sessionMessage = UserHolder.getCurrentSessionMessage();		
		jsonObject = saleButtonService.getButttonsBySatatus(Sale.STATUS_NEW, sessionMessage.getUserId(),sessionMessage.getUserId());
		return SUCCESS;
	}
	
	public String getSaleAndItemsById(){
		jsonObject = new JSONObject();
		
		try {
			jsonObject = saleService.getSaleAndItemsById(Integer.valueOf(queryParam));
			jsonObject.put(SaleConstantEnum.SUCCESS.getType(), true);
			jsonObject.put(SaleConstantEnum.MSG.getType(),"");
		} catch (Exception e) {
			log.error(SaleConstantEnum.OPEN_FAILURE.getType(), e);
			jsonObject.put(SaleConstantEnum.SUCCESS.getType(), false);
			jsonObject.put(SaleConstantEnum.MSG.getType(), SaleConstantEnum.OPEN_FAILURE.getType());
		}
		return SUCCESS;
	}
	/**
	 * 草稿
	 * @return
	 */
	public String create(){
		jsonObject = new JSONObject();
		try {
			jsonObject = saleService.backupCreate(queryParam);
			jsonObject.put(SaleConstantEnum.SUCCESS.getType(), true);
			jsonObject.put(SaleConstantEnum.MSG.getType(),SaleConstantEnum.SAVE_CREATE_SUCCESS.getType());
		} catch (Exception e) {
			log.error(SaleConstantEnum.SAVE_CREATE_FAILURE.getType(), e);
			jsonObject.put(SaleConstantEnum.SUCCESS.getType(), false);
			jsonObject.put(SaleConstantEnum.MSG.getType(), SaleConstantEnum.SAVE_CREATE_FAILURE.getType());
		}
		return SUCCESS;
	}
	/**
	 *待仓库审核
	 * @return
	 */
	public String submit(){
		jsonObject = new JSONObject();
		try {
			jsonObject = saleService.backupSubmit(queryParam);
			jsonObject.put(SaleConstantEnum.SUCCESS.getType(), true);
			jsonObject.put(SaleConstantEnum.MSG.getType(),SaleConstantEnum.SAVE_SUBMIT_SUCCESS.getType());
		} catch (Exception e) {
			log.error(SaleConstantEnum.SAVE_SUBMIT_FAILURE.getType(), e);
			jsonObject.put(SaleConstantEnum.SUCCESS.getType(), false);
			jsonObject.put(SaleConstantEnum.MSG.getType(), SaleConstantEnum.SAVE_SUBMIT_FAILURE.getType());
		}
		return SUCCESS;
	}
	/**
	 * 审核通过,出货
	 * @return
	 */
	public String delivery(){
		jsonObject = new JSONObject();
		try {
			jsonObject = saleService.backupDelivery(queryParam);
			jsonObject.put(SaleConstantEnum.SUCCESS.getType(), true);
			jsonObject.put(SaleConstantEnum.MSG.getType(),SaleConstantEnum.SAVE_DELIVERY_SUCCESS.getType());
		} catch (Exception e) {
			log.error(SaleConstantEnum.SAVE_DELIVERY_FAILURE.getType(), e);
			jsonObject.put(SaleConstantEnum.SUCCESS.getType(), false);
			jsonObject.put(SaleConstantEnum.MSG.getType(), SaleConstantEnum.SAVE_DELIVERY_FAILURE.getType());
		}
		return SUCCESS;
	}
	/**
	 * 接手单据
	 * @return
	 */
	public String acceptSale(){
		sessionMessage = UserHolder.getCurrentSessionMessage();
		jsonObject = new JSONObject();
		try {
			jsonObject = saleService.updateSaleCurrentorId(Integer.valueOf(queryParam),sessionMessage.getUserId());
			jsonObject.put(SaleConstantEnum.SUCCESS.getType(), true);
			jsonObject.put(SaleConstantEnum.MSG.getType(),SaleConstantEnum.ACCEPT_SUCCESS.getType());
		} catch (Exception e) {
			log.error(SaleConstantEnum.ACCEPT_FAILIURE.getType(), e);
			jsonObject.put(SaleConstantEnum.SUCCESS.getType(), false);
			jsonObject.put(SaleConstantEnum.MSG.getType(), SaleConstantEnum.ACCEPT_FAILIURE.getType());
		}
		return SUCCESS;
	}
	/**
	 * 释放单据
	 * @return
	 */
	public String releaseSale(){		
		jsonObject = new JSONObject();
		try {
			jsonObject = saleService.updateSaleCurrentorId(Integer.valueOf(queryParam),null);
			jsonObject.put(SaleConstantEnum.SUCCESS.getType(), true);
			jsonObject.put(SaleConstantEnum.MSG.getType(),SaleConstantEnum.RELEASE_SUCCESS.getType());
		} catch (Exception e) {
			log.error(SaleConstantEnum.RELEASE_FAILIURE.getType(), e);
			jsonObject.put(SaleConstantEnum.SUCCESS.getType(), false);
			jsonObject.put(SaleConstantEnum.MSG.getType(), SaleConstantEnum.RELEASE_FAILIURE.getType());
		}
		return SUCCESS;
	}
	/**
	 * 删除草稿
	 * @return
	 */
	public String deleteCreate(){
		jsonObject = new JSONObject();
		try {
			jsonObject = saleService.deleteSaleById(Integer.valueOf(queryParam));			
		} catch (Exception e) {
			log.error(SaleConstantEnum.DELETECREATE_FAILIURE.getType(), e);
			jsonObject.put(SaleConstantEnum.SUCCESS.getType(), false);
			jsonObject.put(SaleConstantEnum.MSG.getType(), SaleConstantEnum.DELETECREATE_FAILIURE.getType());
		}
		return SUCCESS;
	}
	/**
	 * 待仓库审核状态退回草稿状态
		 * @return
	 */
	public String backToCreate(){
		jsonObject = new JSONObject();
		try {
			jsonObject = saleService.updateSaleStatusById(Integer.valueOf(queryParam));
			jsonObject.put(SaleConstantEnum.SUCCESS.getType(), true);
			jsonObject.put(SaleConstantEnum.MSG.getType(), SaleConstantEnum.BACK_TO_DRAFT_SUCCESS.getType());
		} catch (Exception e) {
			log.error(SaleConstantEnum.BACK_TO_DRAFT_FAILURE.getType(), e);
			jsonObject.put(SaleConstantEnum.SUCCESS.getType(), false);
			jsonObject.put(SaleConstantEnum.MSG.getType(), SaleConstantEnum.BACK_TO_DRAFT_FAILURE.getType());
		}
		return SUCCESS;
	}
	
	public String getOrderDetailForSale(){
		jsonObject = new JSONObject();
		try {
			jsonObject = saleService.generateSaleAndItemsByOrderId(Integer.valueOf(queryParam));
			jsonObject.put("success", true);
		} catch (Exception e) {
			log.error("引用销售订单失败......",e);
			jsonObject.put(SaleConstantEnum.SUCCESS.getType(), false);
			jsonObject.put(SaleConstantEnum.MSG.getType(), "引用销售订单失败......");
		}
		return SUCCESS;
	}
	
	public String acceptOrderToSale(){	
		jsonObject = new JSONObject();
		try {
			jsonObject = saleService.acceptOrderToSaleByOrderId(Integer.valueOf(queryParam));
		} catch (Exception e) {
			log.error(SaleConstantEnum.QUOTE_OREDERS_FAILURE.getType(),e);
		    jsonObject.put(SaleConstantEnum.SUCCESS.getType(), false);
		    jsonObject.put(SaleConstantEnum.MSG.getType(),SaleConstantEnum.QUOTE_OREDERS_FAILURE.getType());
		}					
		return SUCCESS;
	}
	
	public void setSaleService(SaleService saleService) {
		this.saleService = saleService;
	}



	public void setSessionMessage(SessionMessage sessionMessage) {
		this.sessionMessage = sessionMessage;
	}

	public void setQueryParam(String queryParam) {
		this.queryParam = queryParam;
	}

	public JSONObject getJsonObject() {
		return jsonObject;
	}

	public void setJsonObject(JSONObject jsonObject) {
		this.jsonObject = jsonObject;
	}

	public void setSaleButtonService(SaleButtonService saleButtonService) {
		this.saleButtonService = saleButtonService;
	}	

}
